package com.candy.control;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class LoginServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
//        获取初始化参数
        String username=config.getInitParameter("username");
        String password=config.getInitParameter("password");
        System.out.println("获取初始化参数，username:"+username+";password:"+password);
//    获取初始化参数名称
        Enumeration em= config.getInitParameterNames();
        while(em.hasMoreElements()){
            System.out.println("获取的初始化参数名称："+em.nextElement());
        }
//        获取Servlet的name值，即获取配置的servlet-name属性的值
        System.out.println("ServletName:"+config.getServletName());
//        获取ServletContext对象
        ServletContext  context= config.getServletContext();
//        获取Servlet容器中的配置参数（在web.xml中配置的context-param中所配置的参数信息）
        String initParam=context.getInitParameter("ServletContextName");
        System.out.println("ServletContextParam:"+initParam);
//        获取启动路径的信息
        System.out.println("path:"+context.getContextPath());
//        获取其他路径对应ServletContext对象
        System.out.println("LoginServlet2:"+context.getContext("/javaweb20250324/loginServlet"));
//        获取web容器（tomcat)的名字与版本
        System.out.println("info："+context.getServerInfo());
//        获取Servlet版本信息
        System.out.println("version："+context.getMajorVersion());

    }

    //   处理get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        System.out.println("接收到客户端的请求---get");
        String username=req.getParameter("username");
        String userpwd=req.getParameter("userpwd");
        System.out.println("用户名："+username);
        System.out.println("密码："+userpwd);
    }
//处理post请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接收到客户端的请求---post");
    }
}
